const xlsx = require('node-xlsx')

exports.readExcel = (file,log)=>{

	// 解析得到文档中的所有 sheet
	const sheets = xlsx.parse(file);

	const out = [];

	const headers = [
		{
			title:"拍摄序号",
			key:"sort"
		},
		{
			title:"学号",
			key:"num"
		},
		{
			title:"姓名",
			key:"name"
		},
		{
			title:"身份证号",
			key:"idCard"
		},
		{
			title:"院系代码",
			key:"school"
		}
	]
	 
	// 遍历 sheet
	sheets.forEach(function(sheet){

		for(let rowId in sheet['data']){

			const row = sheet['data'][rowId];

			if(rowId == 0){
				headers.forEach(head => {
					head.index = row.indexOf(head.title)
					if(head.index == -1)
						 log.error('excel不存在'+head.title)
				})
				continue;
			}

			const rowData = {};

			headers.forEach(head => {
				rowData[head.key] = row[head.index]
			})

			out.push(rowData)

		}

	  
	});

  return out;
}

// exports.writeExcel = (file,data)=>{
// 	var tool = new xlsx();
// 	// columns will be ordered by ["stux", "foo", "boom"]; column "boom" will be named "hello"
// 	var xls = tool.json2xls(data, {order:["school","name", "idCard", "num"]}); 
// 	fs.writeFileSync(file,xls, 'binary');
// }


// exports.writeExcel('C:\\Users\\yeqin\\Desktop\\学生素材导入\\学信网照片\\notUpload.xls',[{num:"222",school:"daxue",idCard:"2233",name:"name2233"}])